<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Swarm | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/docker_swarm.md.KZrO-Ohf.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>dorker</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>dorker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/Dockerfile.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Dockerfile</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/compose.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker-compose</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/concept.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 基础概念</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/container.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>container</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/images.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>images</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/mac-docker-auto.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Mac下给docker配置命令补全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/multi-architecture.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 的多架构支持</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/network.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/security.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>安全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/storate.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>存储Volume &amp; Bind mount</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/swarm.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Swarm</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _docker_swarm" data-v-39a288b8><div><h1 id="swarm" tabindex="-1">Swarm <a class="header-anchor" href="#swarm" aria-label="Permalink to &quot;Swarm&quot;">​</a></h1><h1 id="激活" tabindex="-1">激活 <a class="header-anchor" href="#激活" aria-label="Permalink to &quot;激活&quot;">​</a></h1><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> info</span><span style="color:#7F848E;font-style:italic;"> # 查看 Swarm: active</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> swarm</span><span style="color:#98C379;"> init</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>相关命令</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 列出节点状态</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> node</span><span style="color:#98C379;"> ls</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># leave</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> node</span><span style="color:#98C379;"> leave</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 创建服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#98C379;"> nignx</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看service </span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看具体service</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ps</span><span style="color:#ABB2BF;"> &lt;</span><span style="color:#98C379;">service_i</span><span style="color:#ABB2BF;">d&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 删除具体service</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> rm</span><span style="color:#ABB2BF;"> &lt;</span><span style="color:#98C379;">service_i</span><span style="color:#ABB2BF;">d&gt;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h1 id="练习" tabindex="-1">练习 <a class="header-anchor" href="#练习" aria-label="Permalink to &quot;练习&quot;">​</a></h1><p>相关脚本 setup.sh</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;">#/bin/sh</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># install some tools</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> apt-get</span><span style="color:#98C379;"> install</span><span style="color:#D19A66;"> -y</span><span style="color:#98C379;"> git</span><span style="color:#98C379;"> vim</span><span style="color:#98C379;"> gcc</span><span style="color:#98C379;"> build-essential</span><span style="color:#98C379;"> telnet</span><span style="color:#98C379;"> bridge-utils</span><span style="color:#98C379;"> ipvsadm</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># install docker</span></span>
<span class="line"><span style="color:#61AFEF;">curl</span><span style="color:#D19A66;"> -fsSL</span><span style="color:#98C379;"> https://gitee.com/loveagri/vagrant/raw/master/docker/get-docker.sh</span><span style="color:#D19A66;"> -o</span><span style="color:#98C379;"> get-docker.sh</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> sh</span><span style="color:#98C379;"> get-docker.sh</span><span style="color:#D19A66;"> --mirror</span><span style="color:#98C379;"> AzureChinaCloud</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">if</span><span style="color:#ABB2BF;"> [ </span><span style="color:#56B6C2;">!</span><span style="color:#56B6C2;"> -f</span><span style="color:#ABB2BF;"> /etc/docker/daemon.json ]; </span><span style="color:#C678DD;">then</span></span>
<span class="line"><span style="color:#61AFEF;">    sudo</span><span style="color:#98C379;"> mkdir</span><span style="color:#D19A66;"> -p</span><span style="color:#98C379;"> /etc/docker</span></span>
<span class="line"><span style="color:#61AFEF;">    cat</span><span style="color:#ABB2BF;"> &lt;&lt;</span><span style="color:#ABB2BF;">EOF</span><span style="color:#ABB2BF;"> | </span><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> tee</span><span style="color:#98C379;"> /etc/docker/daemon.json</span></span>
<span class="line"><span style="color:#98C379;">{</span></span>
<span class="line"><span style="color:#98C379;">    &quot;exec-opts&quot;: [&quot;native.cgroupdriver=systemd&quot;],</span></span>
<span class="line"><span style="color:#98C379;">    &quot;log-driver&quot;: &quot;json-file&quot;,</span></span>
<span class="line"><span style="color:#98C379;">    &quot;log-opts&quot;: {</span></span>
<span class="line"><span style="color:#98C379;">        &quot;max-size&quot;: &quot;100m&quot;</span></span>
<span class="line"><span style="color:#98C379;">    },</span></span>
<span class="line"><span style="color:#98C379;">    &quot;builder&quot;: {</span></span>
<span class="line"><span style="color:#98C379;">    &quot;gc&quot;: {</span></span>
<span class="line"><span style="color:#98C379;">      &quot;defaultKeepStorage&quot;: &quot;20GB&quot;,</span></span>
<span class="line"><span style="color:#98C379;">      &quot;enabled&quot;: true</span></span>
<span class="line"><span style="color:#98C379;">    }</span></span>
<span class="line"><span style="color:#98C379;">  },</span></span>
<span class="line"><span style="color:#98C379;">  &quot;experimental&quot;: false,</span></span>
<span class="line"><span style="color:#98C379;">    &quot;storage-driver&quot;: &quot;overlay2&quot;,</span></span>
<span class="line"><span style="color:#98C379;">    &quot;registry-mirrors&quot;: [</span></span>
<span class="line"><span style="color:#98C379;">        &quot;https://ccr.ccs.tencentyun.com&quot;,</span></span>
<span class="line"><span style="color:#98C379;">        &quot;https://ustc-edu-cn.mirror.aliyuncs.com&quot;,</span></span>
<span class="line"><span style="color:#98C379;">        &quot;https://docker.m.daocloud.io&quot;</span></span>
<span class="line"><span style="color:#98C379;">    ]</span></span>
<span class="line"><span style="color:#98C379;">}</span></span>
<span class="line"><span style="color:#ABB2BF;">EOF</span></span>
<span class="line"><span style="color:#C678DD;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> systemctl</span><span style="color:#98C379;"> start</span><span style="color:#98C379;"> docker</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> systemctl</span><span style="color:#98C379;"> enable</span><span style="color:#98C379;"> docker</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C678DD;">if</span><span style="color:#ABB2BF;"> [ </span><span style="color:#56B6C2;">!</span><span style="color:#ABB2BF;"> $(</span><span style="color:#61AFEF;">getent</span><span style="color:#98C379;"> group</span><span style="color:#98C379;"> docker</span><span style="color:#ABB2BF;">) ]; </span><span style="color:#C678DD;">then</span></span>
<span class="line"><span style="color:#61AFEF;">    sudo</span><span style="color:#98C379;"> groupadd</span><span style="color:#98C379;"> docker</span></span>
<span class="line"><span style="color:#C678DD;">else</span></span>
<span class="line"><span style="color:#56B6C2;">    echo</span><span style="color:#98C379;"> &quot;docker user group already exists&quot;</span></span>
<span class="line"><span style="color:#C678DD;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> gpasswd</span><span style="color:#D19A66;"> -a</span><span style="color:#E06C75;"> $USER</span><span style="color:#98C379;"> docker</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> restart</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">rm</span><span style="color:#D19A66;"> -rf</span><span style="color:#98C379;"> get-docker.sh</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># open password auth for backup if ssh key doesn&#39;t work, bydefault, username=vagrant password=vagrant</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># sudo sed -i &#39;s/PasswordAuthentication no/PasswordAuthentication yes/g&#39; /etc/ssh/sshd_config</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># sudo service sshd restart</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div><p>Vagrantfile</p><div class="language-ruby line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">ruby</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># -*- mode: ruby -*-</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># vi: set ft=ruby :</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">Vagrant</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">require_version</span><span style="color:#98C379;"> &quot;&gt;= 1.6.0&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">boxes</span><span style="color:#ABB2BF;"> = [</span></span>
<span class="line"><span style="color:#ABB2BF;">    {</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">name</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;swarm-manager&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">eth1</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;192.168.200.10&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">mem</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1024&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">cpu</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">    },</span></span>
<span class="line"><span style="color:#ABB2BF;">    {</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">name</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;swarm-worker1&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">eth1</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;192.168.200.11&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">mem</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1024&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">cpu</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">    },</span></span>
<span class="line"><span style="color:#ABB2BF;">    {</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">name</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;swarm-worker2&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">eth1</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;192.168.200.12&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">mem</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1024&quot;</span><span style="color:#ABB2BF;">,</span></span>
<span class="line"><span style="color:#D19A66;">        :</span><span style="color:#56B6C2;">cpu</span><span style="color:#ABB2BF;"> =&gt; </span><span style="color:#98C379;">&quot;1&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">    }</span></span>
<span class="line"><span style="color:#ABB2BF;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E5C07B;">Vagrant</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">configure</span><span style="color:#ABB2BF;">(</span><span style="color:#D19A66;">2</span><span style="color:#ABB2BF;">) </span><span style="color:#C678DD;">do</span><span style="color:#ABB2BF;"> |</span><span style="color:#E06C75;">config</span><span style="color:#ABB2BF;">|</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">  config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">box</span><span style="color:#56B6C2;"> =</span><span style="color:#98C379;"> &quot;ubuntu/focal64&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">  boxes.</span><span style="color:#61AFEF;">each</span><span style="color:#C678DD;"> do</span><span style="color:#ABB2BF;"> |</span><span style="color:#E06C75;">opts</span><span style="color:#ABB2BF;">|</span></span>
<span class="line"><span style="color:#ABB2BF;">    config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">define</span><span style="color:#ABB2BF;"> opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">name</span><span style="color:#ABB2BF;">] </span><span style="color:#C678DD;">do</span><span style="color:#ABB2BF;"> |</span><span style="color:#E06C75;">config</span><span style="color:#ABB2BF;">|</span></span>
<span class="line"><span style="color:#ABB2BF;">      config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">hostname</span><span style="color:#56B6C2;"> =</span><span style="color:#ABB2BF;"> opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">name</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#ABB2BF;">      config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">provider</span><span style="color:#98C379;"> &quot;vmware_fusion&quot;</span><span style="color:#C678DD;"> do</span><span style="color:#ABB2BF;"> |</span><span style="color:#E06C75;">v</span><span style="color:#ABB2BF;">|</span></span>
<span class="line"><span style="color:#ABB2BF;">        v.</span><span style="color:#61AFEF;">vmx</span><span style="color:#ABB2BF;">[</span><span style="color:#98C379;">&quot;memsize&quot;</span><span style="color:#ABB2BF;">] </span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;"> opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">mem</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#ABB2BF;">        v.</span><span style="color:#61AFEF;">vmx</span><span style="color:#ABB2BF;">[</span><span style="color:#98C379;">&quot;numvcpus&quot;</span><span style="color:#ABB2BF;">] </span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;"> opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">cpu</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#C678DD;">      end</span></span>
<span class="line"><span style="color:#ABB2BF;">      config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">provider</span><span style="color:#98C379;"> &quot;virtualbox&quot;</span><span style="color:#C678DD;"> do</span><span style="color:#ABB2BF;"> |</span><span style="color:#E06C75;">v</span><span style="color:#ABB2BF;">|</span></span>
<span class="line"><span style="color:#ABB2BF;">        v.</span><span style="color:#61AFEF;">customize</span><span style="color:#ABB2BF;"> [</span><span style="color:#98C379;">&quot;modifyvm&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">id</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;--memory&quot;</span><span style="color:#ABB2BF;">, opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">mem</span><span style="color:#ABB2BF;">]]</span></span>
<span class="line"><span style="color:#ABB2BF;">        v.</span><span style="color:#61AFEF;">customize</span><span style="color:#ABB2BF;"> [</span><span style="color:#98C379;">&quot;modifyvm&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">id</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;--cpus&quot;</span><span style="color:#ABB2BF;">, opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">cpu</span><span style="color:#ABB2BF;">]]</span></span>
<span class="line"><span style="color:#C678DD;">      end</span></span>
<span class="line"><span style="color:#ABB2BF;">      config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">network</span><span style="color:#D19A66;"> :</span><span style="color:#56B6C2;">private_network</span><span style="color:#ABB2BF;">, </span><span style="color:#56B6C2;">ip</span><span style="color:#D19A66;">:</span><span style="color:#ABB2BF;"> opts[</span><span style="color:#D19A66;">:</span><span style="color:#56B6C2;">eth1</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#C678DD;">    end</span></span>
<span class="line"><span style="color:#C678DD;">  end</span></span>
<span class="line"><span style="color:#ABB2BF;">  config.</span><span style="color:#61AFEF;">vm</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">provision</span><span style="color:#98C379;"> &quot;shell&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#56B6C2;">privileged</span><span style="color:#D19A66;">:</span><span style="color:#D19A66;"> false</span><span style="color:#ABB2BF;">, </span><span style="color:#56B6C2;">path</span><span style="color:#D19A66;">:</span><span style="color:#98C379;"> &quot;./setup.sh&quot;</span></span>
<span class="line"><span style="color:#C678DD;">end</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><p>相关脚本</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 激活manager 中的swarm</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> swarm</span><span style="color:#98C379;"> init</span><span style="color:#D19A66;"> --advertise-addr=192.168.200.10</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># worker 中执行</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> swarm</span><span style="color:#98C379;"> join</span><span style="color:#D19A66;"> --token</span><span style="color:#98C379;"> SWMTKN-1-0mze9shr7wr228urjicco5waoma2h2xgrttvxuvin3sqwke53d-7antikyw5gxzlctdqci8psecu</span><span style="color:#98C379;"> 192.168.200.10:2377</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看节点</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> node</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 创建服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> web</span><span style="color:#98C379;"> nginx</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># list service</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># show service detail and show which node was created a web service</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ps</span><span style="color:#98C379;"> web</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># duplicate service </span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> update</span><span style="color:#98C379;"> web</span><span style="color:#D19A66;"> --replicas</span><span style="color:#D19A66;"> 3</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># scale</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> scale</span><span style="color:#98C379;"> web=</span><span style="color:#D19A66;">4</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># logs</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> logs</span><span style="color:#98C379;"> web</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h3 id="网络相关" tabindex="-1">网络相关 <a class="header-anchor" href="#网络相关" aria-label="Permalink to &quot;网络相关&quot;">​</a></h3><p>对于理解swarm的网络来讲，个人认为最重要的两个点：</p><ul><li>第一是外部如何访问部署运行在swarm集群内的服务，可以称之为 <code>入方向</code> 流量，在swarm里我们通过 <code>ingress</code> 来解决</li><li>第二是部署在swarm集群里的服务，如何对外进行访问，这部分又分为两块: <ul><li>第一，<code>东西向流量</code> ，也就是不同swarm节点上的容器之间如何通信，swarm通过 <code>overlay</code> 网络来解决；</li><li>第二，<code>南北向流量</code> ，也就是swarm集群里的容器如何对外访问，比如互联网，这个是 <code>Linux bridge + iptables NAT</code> 来解决的</li></ul></li></ul><div class="tip custom-block"><p class="custom-block-title">提示</p><p>overlay网络是节点之间进行通信的，bridge网络是对外通信</p></div><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># create overlay network</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> -d</span><span style="color:#98C379;"> overlay</span><span style="color:#98C379;"> mynet</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># create service with overlay network</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mynet</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> test</span><span style="color:#D19A66;"> --replicas</span><span style="color:#D19A66;"> 2</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> ping</span><span style="color:#D19A66;"> 8.8.8.8</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># show ip route</span></span>
<span class="line"><span style="color:#61AFEF;">ip</span><span style="color:#98C379;"> route</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">default</span><span style="color:#98C379;"> via</span><span style="color:#D19A66;"> 172.18.0.1</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth1</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">10.0.1.0/24</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth0</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;">  src</span><span style="color:#D19A66;"> 10.0.1.3</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">172.18.0.0/16</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth1</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;">  src</span><span style="color:#D19A66;"> 172.18.0.3</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># grap the data package on node</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> tcpdump</span><span style="color:#D19A66;"> -i</span><span style="color:#98C379;"> enp0s8</span><span style="color:#98C379;"> port</span><span style="color:#D19A66;"> 4789</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="ingress网络" tabindex="-1">ingress网络 <a class="header-anchor" href="#ingress网络" aria-label="Permalink to &quot;ingress网络&quot;">​</a></h2><p>docker swarm的ingress网络又叫 <code>Ingress Routing Mesh</code>，实现把service的服务端口对外发布出去，让其能够被外部网络访问到。</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># create service</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> web</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mynet</span><span style="color:#D19A66;"> -p</span><span style="color:#98C379;"> 8080:80</span><span style="color:#D19A66;"> --replicas</span><span style="color:#D19A66;"> 2</span><span style="color:#98C379;"> containous/whoami</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># request on three nodes</span></span>
<span class="line"><span style="color:#61AFEF;">curl</span><span style="color:#98C379;"> 192.168.200.10:8080</span></span>
<span class="line"><span style="color:#61AFEF;">curl</span><span style="color:#98C379;"> 192.168.200.11:8080</span></span>
<span class="line"><span style="color:#61AFEF;">curl</span><span style="color:#98C379;"> 192.168.200.12:8080</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="ingress-数据包的走向" tabindex="-1">ingress 数据包的走向 <a class="header-anchor" href="#ingress-数据包的走向" aria-label="Permalink to &quot;ingress 数据包的走向&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># show data flow</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> iptables</span><span style="color:#D19A66;"> -nvL</span><span style="color:#D19A66;"> -t</span><span style="color:#98C379;"> nat</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> DOCKER-INGRESS</span><span style="color:#ABB2BF;"> (2 </span><span style="color:#98C379;">references</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;"> pkts</span><span style="color:#98C379;"> bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">    6</span><span style="color:#D19A66;">   360</span><span style="color:#98C379;"> DNAT</span><span style="color:#98C379;">       tcp</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#E5C07B;">      *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">            0.0.0.0/0</span><span style="color:#98C379;">            tcp</span><span style="color:#98C379;"> dpt:8080</span><span style="color:#98C379;"> to:172.18.0.2:8080</span></span>
<span class="line"><span style="color:#61AFEF;">   41</span><span style="color:#D19A66;">  2760</span><span style="color:#98C379;"> RETURN</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#E5C07B;">      *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">            0.0.0.0/0</span><span style="color:#ABB2BF;">     </span></span>
<span class="line"><span style="color:#ABB2BF;">   </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 172.18.0.0/16　这个网段是 docker_gwbridge 的，所以这个地址肯定是连在了 docker_gwbridge 上。</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># check docker_gwbridge</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> inspect</span><span style="color:#98C379;"> docker_gwbridge</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">&quot;Containers&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">            &quot;9da6f4b47253f34ca525ef207d2a67cfb3e8b326d006ce8ef1bc28445d20acd0&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;Name&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;gateway_e67f8adcdcb6&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;EndpointID&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;b3ab9627a02842c10ad2e620aba01dcc06c00dc8dbbe2459c8b36dc6695c2494&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;MacAddress&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;02:42:ac:12:00:03&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;IPv4Address&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;172.18.0.3/16&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;IPv6Address&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">            },</span></span>
<span class="line"><span style="color:#61AFEF;">            &quot;ingress-sbox&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> {</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;Name&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;gateway_ingress-sbox&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;EndpointID&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;e61832535044310758548272cc553b3f3f9c0f3c92d301b3ccff9830f6940f5b&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;MacAddress&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;02:42:ac:12:00:02&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;IPv4Address&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;172.18.0.2/16&quot;,</span></span>
<span class="line"><span style="color:#61AFEF;">                &quot;IPv6Address&quot;</span><span style="color:#56B6C2;">:</span><span style="color:#98C379;"> &quot;&quot;</span></span>
<span class="line"><span style="color:#ABB2BF;">            }</span></span>
<span class="line"><span style="color:#ABB2BF;">        },</span></span>
<span class="line"><span style="color:#ABB2BF;">        </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># a network namespace -&gt; ingress-sbox</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># entry the network namespace with a container</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -it</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> -v</span><span style="color:#98C379;"> /var/run/docker/netns:/netns</span><span style="color:#D19A66;"> --privileged=true</span><span style="color:#98C379;"> nicolaka/netshoot</span><span style="color:#98C379;"> nsenter</span><span style="color:#D19A66;"> --net=/netns/ingress_sbox</span><span style="color:#98C379;"> sh</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># into the container and check dest</span></span>
<span class="line"><span style="color:#61AFEF;">iptables</span><span style="color:#D19A66;"> -nvL</span><span style="color:#D19A66;"> -t</span><span style="color:#98C379;"> mangle</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># show load balancing</span></span>
<span class="line"><span style="color:#61AFEF;">ipvsadm</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><h2 id="内部负载均衡和-vip" tabindex="-1">内部负载均衡和 VIP <a class="header-anchor" href="#内部负载均衡和-vip" aria-label="Permalink to &quot;内部负载均衡和 VIP&quot;">​</a></h2><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># web</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> web</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mynet</span><span style="color:#D19A66;"> --replicas</span><span style="color:#D19A66;"> 2</span><span style="color:#98C379;"> containous/whoami</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ps</span><span style="color:#98C379;"> web</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># client</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> client</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mynet</span><span style="color:#98C379;"> xiaopeng163/net-box:latest</span><span style="color:#98C379;"> ping</span><span style="color:#D19A66;"> 8.8.8.8</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> ps</span><span style="color:#98C379;"> client</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 找到client容器的部署节点 exec 进入,curl web 显示负载均衡的效果</span></span>
<span class="line"><span style="color:#61AFEF;">/omd</span><span style="color:#7F848E;font-style:italic;"> # curl web</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>这个虚拟IP在一个特殊的网络命令空间里，这个空间连接在我们的mynet这个overlay的网络上</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 通过 docker network inspect mynet 可以看到这个命名空间，叫lb-mynet</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> inspect</span><span style="color:#98C379;"> mynet</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># show lb_viuh1fo7q network interface</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> ls</span><span style="color:#98C379;"> /var/run/docker/netns/</span></span>
<span class="line"><span style="color:#61AFEF;">1-14fy2l7a4m</span><span style="color:#98C379;">  1-lpirdge00y</span><span style="color:#98C379;">  dfb766d83076</span><span style="color:#98C379;">  ingress_sbox</span><span style="color:#98C379;">  lb_viuh1fo7q</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># entry lb_lpirdge00 namespace</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> nsenter</span><span style="color:#D19A66;"> --net=/var/run/docker/netns/lb_viuh1fo7q</span><span style="color:#98C379;"> sh</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">ip</span><span style="color:#98C379;"> a</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># console output</span></span>
<span class="line"><span style="color:#61AFEF;">1:</span><span style="color:#98C379;"> lo:</span><span style="color:#ABB2BF;"> &lt;</span><span style="color:#98C379;">LOOPBACK,UP,LOWER_U</span><span style="color:#ABB2BF;">P&gt; </span><span style="color:#98C379;">mtu</span><span style="color:#D19A66;"> 65536</span><span style="color:#98C379;"> qdisc</span><span style="color:#98C379;"> noqueue</span><span style="color:#98C379;"> state</span><span style="color:#98C379;"> UNKNOWN</span><span style="color:#98C379;"> group</span><span style="color:#98C379;"> default</span><span style="color:#98C379;"> qlen</span><span style="color:#D19A66;"> 1000</span></span>
<span class="line"><span style="color:#61AFEF;">    link/loopback</span><span style="color:#98C379;"> 00:00:00:00:00:00</span><span style="color:#98C379;"> brd</span><span style="color:#98C379;"> 00:00:00:00:00:00</span></span>
<span class="line"><span style="color:#61AFEF;">    inet</span><span style="color:#98C379;"> 127.0.0.1/8</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> host</span><span style="color:#98C379;"> lo</span></span>
<span class="line"><span style="color:#61AFEF;">       valid_lft</span><span style="color:#98C379;"> forever</span><span style="color:#98C379;"> preferred_lft</span><span style="color:#98C379;"> forever</span></span>
<span class="line"><span style="color:#61AFEF;">    inet6</span><span style="color:#98C379;"> ::1/128</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> host</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">       valid_lft</span><span style="color:#98C379;"> forever</span><span style="color:#98C379;"> preferred_lft</span><span style="color:#98C379;"> forever</span></span>
<span class="line"><span style="color:#61AFEF;">27:</span><span style="color:#98C379;"> eth0@if28:</span><span style="color:#ABB2BF;"> &lt;</span><span style="color:#98C379;">BROADCAST,MULTICAST,UP,LOWER_U</span><span style="color:#ABB2BF;">P&gt; </span><span style="color:#98C379;">mtu</span><span style="color:#D19A66;"> 1450</span><span style="color:#98C379;"> qdisc</span><span style="color:#98C379;"> noqueue</span><span style="color:#98C379;"> state</span><span style="color:#98C379;"> UP</span><span style="color:#98C379;"> group</span><span style="color:#98C379;"> default</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">    link/ether</span><span style="color:#98C379;"> 02:42:0a:00:01:08</span><span style="color:#98C379;"> brd</span><span style="color:#98C379;"> ff:ff:ff:ff:ff:ff</span><span style="color:#98C379;"> link-netnsid</span><span style="color:#D19A66;"> 0</span></span>
<span class="line"><span style="color:#61AFEF;">    inet</span><span style="color:#98C379;"> 10.0.1.8/24</span><span style="color:#98C379;"> brd</span><span style="color:#D19A66;"> 10.0.1.255</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> global</span><span style="color:#98C379;"> eth0</span></span>
<span class="line"><span style="color:#61AFEF;">       valid_lft</span><span style="color:#98C379;"> forever</span><span style="color:#98C379;"> preferred_lft</span><span style="color:#98C379;"> forever</span></span>
<span class="line"><span style="color:#61AFEF;">    inet</span><span style="color:#98C379;"> 10.0.1.5/32</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> global</span><span style="color:#98C379;"> eth0</span></span>
<span class="line"><span style="color:#61AFEF;">       valid_lft</span><span style="color:#98C379;"> forever</span><span style="color:#98C379;"> preferred_lft</span><span style="color:#98C379;"> forever</span></span>
<span class="line"><span style="color:#61AFEF;">    inet</span><span style="color:#98C379;"> 10.0.1.10/32</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> global</span><span style="color:#98C379;"> eth0</span></span>
<span class="line"><span style="color:#61AFEF;">       valid_lft</span><span style="color:#98C379;"> forever</span><span style="color:#98C379;"> preferred_lft</span><span style="color:#98C379;"> forever</span></span>
<span class="line"><span style="color:#ABB2BF;">       </span></span>
<span class="line"><span style="color:#ABB2BF;">       </span></span>
<span class="line"><span style="color:#61AFEF;">iptables</span><span style="color:#D19A66;"> -nvL</span><span style="color:#D19A66;"> -t</span><span style="color:#98C379;"> mangle</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">ipvsadm</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h2 id="stack-部署多-service-应用" tabindex="-1">stack 部署多 service 应用 <a class="header-anchor" href="#stack-部署多-service-应用" aria-label="Permalink to &quot;stack 部署多 service 应用&quot;">​</a></h2><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># install docker-compose</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> curl</span><span style="color:#D19A66;"> -L</span><span style="color:#98C379;"> &quot;https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(</span><span style="color:#61AFEF;">uname</span><span style="color:#D19A66;"> -s</span><span style="color:#98C379;">)-$(</span><span style="color:#61AFEF;">uname</span><span style="color:#D19A66;"> -m</span><span style="color:#98C379;">)&quot;</span><span style="color:#D19A66;"> -o</span><span style="color:#98C379;"> /usr/local/bin/docker-compose</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> chmod</span><span style="color:#98C379;"> +x</span><span style="color:#98C379;"> /usr/local/bin/docker-compose</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># download flask script</span></span>
<span class="line"><span style="color:#61AFEF;">git</span><span style="color:#98C379;"> clone</span><span style="color:#98C379;"> https://github.com/xiaopeng163/flask-redis</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 通过stack启动服务</span></span>
<span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~/flask-redis$</span><span style="color:#98C379;"> env</span><span style="color:#98C379;"> REDIS_PASSWORD=ABC123</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> stack</span><span style="color:#98C379;"> deploy</span><span style="color:#D19A66;"> --compose-file</span><span style="color:#98C379;"> docker-compose.yml</span><span style="color:#98C379;"> flask-demo</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> stack</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> stack</span><span style="color:#98C379;"> services</span><span style="color:#98C379;"> flask-demo</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">curl</span><span style="color:#98C379;"> 127.0.0.1:8080</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="swarm-中使用-secret" tabindex="-1">swarm 中使用 secret <a class="header-anchor" href="#swarm-中使用-secret" aria-label="Permalink to &quot;swarm 中使用 secret&quot;">​</a></h2><h4 id="从标准的收入读取" tabindex="-1">从标准的收入读取 <a class="header-anchor" href="#从标准的收入读取" aria-label="Permalink to &quot;从标准的收入读取&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># create</span></span>
<span class="line"><span style="color:#56B6C2;">echo</span><span style="color:#98C379;"> abc123</span><span style="color:#ABB2BF;"> | </span><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> secret</span><span style="color:#98C379;"> create</span><span style="color:#98C379;"> mysql_pass</span><span style="color:#98C379;"> -</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># check</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> secret</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># rm</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> secret</span><span style="color:#98C379;"> rm</span><span style="color:#98C379;"> mysql_pass</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="从文件读取" tabindex="-1">从文件读取 <a class="header-anchor" href="#从文件读取" aria-label="Permalink to &quot;从文件读取&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> more</span><span style="color:#98C379;"> mysql_pass.txt</span></span>
<span class="line"><span style="color:#61AFEF;">abc123</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> secret</span><span style="color:#98C379;"> create</span><span style="color:#98C379;"> mysql_pass</span><span style="color:#98C379;"> mysql_pass.txt</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="secret-的使用" tabindex="-1">secret 的使用 <a class="header-anchor" href="#secret-的使用" aria-label="Permalink to &quot;secret 的使用&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 一般在节点下容器中 /run/secrets/下</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> service</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> mysql-demo</span><span style="color:#D19A66;"> --secret</span><span style="color:#98C379;"> mysql_pass</span><span style="color:#D19A66;"> --env</span><span style="color:#98C379;"> MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_pass</span><span style="color:#98C379;"> mysql:5.7</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="local-volume" tabindex="-1">local volume <a class="header-anchor" href="#local-volume" aria-label="Permalink to &quot;local volume&quot;">​</a></h2><p>docker-compose.yml</p><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;3.8&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  db</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">mysql:5.7</span></span>
<span class="line"><span style="color:#E06C75;">    environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_pass</span></span>
<span class="line"><span style="color:#E06C75;">    secrets</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">mysql_pass</span></span>
<span class="line"><span style="color:#E06C75;">    volumes</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">data:/var/lib/mysql</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">volumes</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  data</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">secrets</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  mysql_pass</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    file</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">mysql_pass.txt</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>mysql_pass.txt</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">vagrant@swarm-manager:~$</span><span style="color:#98C379;"> more</span><span style="color:#98C379;"> mysql_pass.txt</span></span>
<span class="line"><span style="color:#61AFEF;">abc123</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/docker/storate.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>存储Volume &amp; Bind mount</span><!--]--></a></div><div class="pager" data-v-e257564d><!----></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>